import { defineMock } from "./base";

export default defineMock([
  {
    url: "family-user/page",
    method: "get",
    body: ({ query }) => {
      const { pageNum = 1, pageSize = 10, name = "", gender = null, alive = null } = query;
      
      // 生成模拟数据
      const generateMemberList = (count = 50) => {
        const list = [];
        for (let i = 0; i < count; i++) {
          const genderValue = Math.random() > 0.5 ? 1 : 2;
          const aliveValue = Math.random() > 0.3 ? 1 : 0;
          const isSelf = i === 0; // 第一个为本人
          
          list.push({
            id: `member_${i + 1}`,
            firstName: `张${i + 1}`,
            lastName: `三${i + 1}`,
            gender: genderValue,
            alive: aliveValue,
            birthDate: aliveValue === 1 ? `198${i % 10}-0${(i % 12) + 1}-${(i % 28) + 1}` : `196${i % 10}-0${(i % 12) + 1}-${(i % 28) + 1}`,
            deathDate: aliveValue === 0 ? `202${i % 5}-0${(i % 12) + 1}-${(i % 28) + 1}` : null,
            idCard: `110101198${i % 10}0${(i % 12) + 1}${(i % 28) + 1}${String(i).padStart(4, '0')}`,
            phone: `138${String(i).padStart(8, '0')}`,
            address: `北京市朝阳区第${i + 1}街道`,
            occupation: ['教师', '医生', '工程师', '商人', '农民', '学生', '退休'][i % 7],
            education: ['小学', '初中', '高中', '大专', '本科', '硕士', '博士'][i % 7],
            remark: `这是第${i + 1}个成员的备注信息`,
            isSelf: isSelf,
            flag: isSelf ? 0 : (i % 3 === 0 ? 1 : 0), // 0-普通成员，1-配偶
            relation: isSelf ? '本人' : ['父亲', '母亲', '儿子', '女儿', '哥哥', '姐姐', '弟弟', '妹妹'][i % 8],
            createTime: `2024-0${(i % 12) + 1}-${(i % 28) + 1} 10:30:00`,
            updateTime: `2024-0${(i % 12) + 1}-${(i % 28) + 1} 15:45:00`,
            createBy: `user_${i + 1}`,
            updateBy: `user_${i + 1}`
          });
        }
        return list;
      };

      let list = generateMemberList(50);

      // 根据查询条件过滤
      if (name) {
        list = list.filter(item => 
          (item.firstName + item.lastName).includes(name)
        );
      }
      
      if (gender !== null && gender !== undefined && gender !== '') {
        list = list.filter(item => item.gender === parseInt(gender));
      }
      
      if (alive !== null && alive !== undefined && alive !== '') {
        list = list.filter(item => item.alive === parseInt(alive));
      }

      const total = list.length;
      const start = (pageNum - 1) * pageSize;
      const end = start + pageSize;
      const pageList = list.slice(start, end);

      return {
        code: 200,
        message: "操作成功",
        data: {
          list: pageList,
          total: total,
          pageNum: pageNum,
          pageSize: pageSize
        }
      };
    }
  },
  {
    url: "family-user/:id",
    method: "get",
    body: ({ params }) => {
      const { id } = params;
      const i = parseInt(id.replace('member_', '')) || 1;
      
      return {
        code: 200,
        message: "操作成功",
        data: {
          id: id,
          firstName: `张${i}`,
          lastName: `三${i}`,
          gender: i % 2 === 0 ? 1 : 2,
          alive: i % 3 === 0 ? 0 : 1,
          birthDate: `198${i % 10}-0${(i % 12) + 1}-${(i % 28) + 1}`,
          deathDate: i % 3 === 0 ? `202${i % 5}-0${(i % 12) + 1}-${(i % 28) + 1}` : null,
          idCard: `110101198${i % 10}0${(i % 12) + 1}${(i % 28) + 1}${String(i).padStart(4, '0')}`,
          phone: `138${String(i).padStart(8, '0')}`,
          address: `北京市朝阳区第${i}街道`,
          occupation: ['教师', '医生', '工程师', '商人', '农民', '学生', '退休'][i % 7],
          education: ['小学', '初中', '高中', '大专', '本科', '硕士', '博士'][i % 7],
          remark: `这是第${i}个成员的备注信息`,
          createTime: `2024-0${(i % 12) + 1}-${(i % 28) + 1} 10:30:00`,
          updateTime: `2024-0${(i % 12) + 1}-${(i % 28) + 1} 15:45:00`,
          createBy: `user_${i}`,
          updateBy: `user_${i}`
        }
      };
    }
  },
  {
    url: "family-user",
    method: "post",
    body: () => {
      return {
        code: 200,
        message: "创建成功",
        data: null
      };
    }
  },
  {
    url: "family-user/:id",
    method: "put",
    body: () => {
      return {
        code: 200,
        message: "更新成功",
        data: null
      };
    }
  },
  {
    url: "family-user/:ids",
    method: "delete",
    body: () => {
      return {
        code: 200,
        message: "删除成功",
        data: null
      };
    }
  },
  {
    url: "family-user/detail/:id",
    method: "get",
    body: ({ params }) => {
      const { id } = params;
      const i = parseInt(id.replace('member_', '')) || 1;
      
      return {
        code: 200,
        message: "操作成功",
        data: {
          id: id,
          firstName: `张${i}`,
          lastName: `三${i}`,
          gender: i % 2 === 0 ? 1 : 2,
          alive: i % 3 === 0 ? 0 : 1,
          birthDate: `198${i % 10}-0${(i % 12) + 1}-${(i % 28) + 1}`,
          deathDate: i % 3 === 0 ? `202${i % 5}-0${(i % 12) + 1}-${(i % 28) + 1}` : null,
          idCard: `110101198${i % 10}0${(i % 12) + 1}${(i % 28) + 1}${String(i).padStart(4, '0')}`,
          phone: `138${String(i).padStart(8, '0')}`,
          address: `北京市朝阳区第${i}街道`,
          occupation: ['教师', '医生', '工程师', '商人', '农民', '学生', '退休'][i % 7],
          education: ['小学', '初中', '高中', '大专', '本科', '硕士', '博士'][i % 7],
          remark: `这是第${i}个成员的备注信息`,
          createTime: `2024-0${(i % 12) + 1}-${(i % 28) + 1} 10:30:00`,
          updateTime: `2024-0${(i % 12) + 1}-${(i % 28) + 1} 15:45:00`,
          createBy: `user_${i}`,
          updateBy: `user_${i}`
        }
      };
    }
  }
]); 